#include "stdafx.h"
#include "jwsmtp/jwsmtp.h"

//libs\ChilkatDbg.lib ws2_32.lib Crypt32.Lib  rpcrt4.lib wininet.lib dnsapi.lib 
//libs\ChilkatRel.lib ws2_32.lib Crypt32.Lib  rpcrt4.lib wininet.lib dnsapi.lib 

//************************************
// Method:    SendEmail
// FullName:  SendEmail
// Access:    public 
// Returns:   bool
// Qualifier:
// Parameter: const char * smtphost
// Parameter: const char * smtpuser
// Parameter: const char * smtppwd
// Parameter: bool bTls
// Parameter: const char * body
// Parameter: vector<CString> & toAddr
// Parameter: const char * fromAddr
// Parameter: const char * subject
//************************************
bool SendEmail(
const char	*smtphost,
const char	*smtpuser,
const char	*smtppwd,
const unsigned short port,
bool		bTls,
const char	*body,
vector<CString> &toAddr,
const char	*fromAddr,
const char	*subject)
{
	jwsmtp::mailer m(toAddr[0], fromAddr, subject,"", smtphost,port, false); //jwsmtp::mailer::SMTP_PORT

	m.setmessage(body);
	m.username(smtpuser);
	m.password(smtppwd);

	vector<CString>::iterator	it_toAddr =  toAddr.begin()+1;

	for( ; it_toAddr != toAddr.end(); it_toAddr++) {
		string	toAddress = *it_toAddr;
		m.addrecipient(toAddress, jwsmtp::mailer::Cc); 
	}
	m.send(); // send the mail
	std::string ret = m.response();
	return ret.substr(0,3) == "250";


/*
	CkMailMan mailman;

	// Any string passed to UnlockComponent begins the 30-day trial.
	bool unlocked = mailman.UnlockComponent("30-day trial");
	if (!unlocked)
	{
		printf("Failed to unlock component\n");
		return false;
	}
	mailman.put_SmtpHost(smtphost);
	mailman.put_SmtpUsername(smtpuser);
	mailman.put_SmtpPassword(smtppwd);

	if( bTls )
		mailman.put_StartTLS(true);


	// Create the email, add the body, recipients, subject, etc.
	CkEmail email;
	vector<CString>::iterator	it_toAddr;

	email.put_Body(body);
	for(it_toAddr = toAddr.begin(); it_toAddr != toAddr.end(); it_toAddr++) {
		email.AddTo("",*it_toAddr);
	}

	email.put_FromAddress(fromAddr);
	email.put_Subject(subject);

	// Send the email
	if (!mailman.SendEmail(&email))
	{
		mailman.SaveLastError("errors.xml");
		return false;
	}
	return true;
/**/
}

